package org.action.order.sercive.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.action.order.domain.entity.ActionUserPay;
import org.action.order.sercive.IActionUserPayService;
import org.action.order.domain.entity.ActionTicketUser;
import org.action.order.domain.pojo.ActionOrder;
import org.action.order.mapper.ActionTicketUserMapper;
import org.common.util.RedisCommon;
import org.museum.api.client.ActionInventoryClient;
import org.museum.api.client.IdentityNameClient;
import org.museum.api.domain.IdentityName;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import static org.action.order.util.StaticVariable.Action_User_Pay_Id;
import static org.action.order.util.StaticVariable.Lock_Action_Order_Key;


@Service
public class ActionOrderProcessingService extends ServiceImpl<ActionTicketUserMapper, ActionTicketUser> {
    @Autowired
    private IdentityNameClient identityNameClient;
    @Autowired
    private ActionInventoryClient actionInventoryClient;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private RedisCommon redisCommon;
    @Autowired
    private IActionUserPayService actionUserPayService;

    //执行活动库存扣减操作
    @Transactional
    public void deductActionTicket(ActionOrder actionOrder){
        //获取锁
        RLock lock = redissonClient.getLock(Lock_Action_Order_Key+ actionOrder.getPhone());
        if(!lock.tryLock()){
            return;
        }
        try{
            //判断是否报过名
            if(checkIsOrder(actionOrder)){
                return;
            }
            //添加身份
            identityNameClient.addIdentityByName(new IdentityName(actionOrder.getRelativeIdentity(),actionOrder.getRelativeName()));
            identityNameClient.addIdentityByName(new IdentityName(actionOrder.getChildrenIdentity(),actionOrder.getChildrenName()));
            System.out.println("开始执行活动库存扣减操作");
            //执行活动库存扣减操作
            if(actionInventoryClient.deductActionTicket(actionOrder.getActionTicketId()).get("code").equals(200)){
                System.out.println("活动库存扣减成功");
                //扣减成功，添加订单
                ActionTicketUser actionTicketUser= BeanUtil.toBean(actionOrder,ActionTicketUser.class);
                actionTicketUser.setCreateTime(LocalDateTime.now());
                //添加订单
                System.out.println(actionTicketUser);
                baseMapper.insert(actionTicketUser);
                //添加用户支付订单
                actionUserPayService.save(getUserPayOrder(actionOrder));
                //return Result.success("活动报名成功");
            }
        }finally {
            lock.unlock();
        }
    }
    //判断是否报过名
    private boolean checkIsOrder(ActionOrder actionOrder){
        return baseMapper.exists(new QueryWrapper<ActionTicketUser>()
                .eq("action_ticket_id",actionOrder.getActionTicketId())
                .and(wrapper -> wrapper
                        .eq("relative_identity", actionOrder.getRelativeIdentity())
                        .or()
                        .eq("children_identity", actionOrder.getChildrenIdentity())
                )
        );
    }

    //获取用户支付订单
    private ActionUserPay getUserPayOrder(ActionOrder actionOrder){
        ActionUserPay actionUserPay=new ActionUserPay();
        actionUserPay.setId(redisCommon.nextId(Action_User_Pay_Id));
        actionUserPay.setUserId(actionOrder.getPhone());
        actionUserPay.setOrderId(actionOrder.getId());
        actionUserPay.setPayStatus(0);
        actionUserPay.setPayCreate(LocalDateTime.now());
        actionUserPay.setPayAmount(actionOrder.getPrice());
        return actionUserPay;
    }


}
